@extends('master')

@include('component.loading')

@section('title', '登录')

@section('content')
<div class="weui_cells_title"></div>
<div class="weui_cells weui_cells_form">
  <div class="weui_cell">
      <div class="weui_cell_hd"><label class="weui_label">帐号</label></div>
      <div class="weui_cell_bd weui_cell_primary">
          <input class="weui_input" type="tel" placeholder="邮箱或手机号" name="email_phone" id="email_phone"/>
      </div>
  </div>
  <div class="weui_cell">
      <div class="weui_cell_hd"><label class="weui_label">密码</label></div>
      <div class="weui_cell_bd weui_cell_primary">
          <input class="weui_input" type="tel" placeholder="不少于6位" name="password" id="password"/>
      </div>
  </div>
  <div class="weui_cell weui_vcode">
      <div class="weui_cell_hd"><label class="weui_label">验证码</label></div>
      <div class="weui_cell_bd weui_cell_primary">
          <input class="weui_input" type="text" placeholder="请输入验证码" name="validate_code" id="validate_code"/>
      </div>
      <div class="weui_cell_ft">
          <img src="/service/validate_code/create" class="bk_validate_code"/>
      </div>
  </div>
</div>
<div class="weui_cells_tips"></div>
<div class="weui_btn_area">
  <a class="weui_btn weui_btn_primary" href="javascript:" onclick="onLoginClick();">登录</a>
</div>
<a href="/register" class="bk_bottom_tips bk_important">没有帐号? 去注册</a>
@endsection

@section('my-js')
<script type="text/javascript">
  $('.bk_title_content').html('登录');
  $('.bk_validate_code').click(function () {
    $(this).attr('src', '/service/validate_code/create?random=' + Math.random());
  });
  function onLoginClick() {
	          email_phone = $('#email_phone').val();
	          password = $('#password').val();
	          validate_code = $('#validate_code').val();
	       if(verifyEmailPhone(email_phone, password,validate_code) == false) {
	           return;
	          }
	        $.ajax({
	          type: "POST",
	          url: '/service/login',
	          dataType: 'json',
	          cache: false,
	          data: {email_phone: email_phone, password: password, validate_code: validate_code, _token: "{{csrf_token()}}"},
	          success: function(data) {
		         
	            if(data == null) {
	              $('.bk_toptips').show();
	              $('.bk_toptips span').html('服务端错误');
	              setTimeout(function() {$('.bk_toptips').hide();}, 2000);
	              return;
	            }
	            if(data.status != 0) {
	              $('.bk_toptips').show();
	              $('.bk_toptips span').html(data.message);
	              setTimeout(function() {$('.bk_toptips').hide();}, 2000);
	              return;
	            }

	            $('.bk_toptips').show();
	            $('.bk_toptips span').html('登录成功');
	            setTimeout(function() {$('.bk_toptips').hide();}, 2000);
	             if("{{$return_url}}"!='') {location.href="{{$return_url}}";}else{
                     location.href='/category';
                   }
	           },
	          error: function(xhr, status, error) {
	            console.log(xhr);
	            console.log(status);
	            console.log(error);
	          }
	        });
	      }

	  function verifyEmailPhone(email_phone, password, validate_code) {
	    // 手机号不为空
	   if(email_phone == '') {
	      $('.bk_toptips').show();
	      $('.bk_toptips span').html('请输入邮箱地址或手机号');
	      setTimeout(function() {$('.bk_toptips').hide();}, 2000);
	      return false;
	    }
	   if(email_phone.indexOf('@')>0 && email_phone.indexOf('.')>0 || email_phone[0]=='1' && email_phone.length==11){
		   if(password.length< 6) {
			      $('.bk_toptips').show();
			      $('.bk_toptips span').html('密码不能少于6位');
			      setTimeout(function() {$('.bk_toptips').hide();}, 2000);
			      return false;
			    }
			    if(validate_code=='') {
			      $('.bk_toptips').show();
			      $('.bk_toptips span').html('验证码不能为空!');
			      setTimeout(function() {$('.bk_toptips').hide();}, 2000);
			      return false;
			    }
			    if(validate_code.length != 4) {
			      $('.bk_toptips').show();
			      $('.bk_toptips span').html('验证码为4位!');
			      setTimeout(function() {$('.bk_toptips').hide();}, 2000);
			      return false;
			    }
	   }else{
		   $('.bk_toptips').show();
		   $('.bk_toptips span').html('邮箱或手机号格式不正确');
		   setTimeout(function() {$('.bk_toptips').hide();}, 2000);
		   return false;
	   }
	    
	    return true;
	  }  
</script>
@endsection
